﻿from pycad.system import *
from pycad.runtime import *

@command()
def pltest():
    res = ed.GetEntity("\n请选择多段线:")
    if res.Status == aced.PromptStatus.OK:
        with dbtrans() as tr:
            pl = tr.getobject(res.ObjectId)

@command()
def mymtext():
    res = ed.GetEntity("\n请选择文字:")
    if res.Status == aced.PromptStatus.OK:
        with dbtrans() as tr:
            txt = tr.getobject(res.ObjectId)
            with upopen(txt): 
                if isinstance(txt, acdb.DBText):
                    txt.TextString = 'abc'
                elif isinstance(txt, acdb.MText):
                    txt.Contents = 'abc'

@command()
def helloworld():
    print 'hello world!'
    with dbtrans() as tr:
        btr = tr.opencurrspace()
        tr.addentity(btr, dr.Line((0,0,0), (10,10,0)))

@command()
def cmdtest():
    from random import uniform
    ed.Command('._line', acge.Point3d(uniform(0, 100), uniform(0, 100), 0), acge.Point3d(uniform(0, 100), uniform(0, 100), 0), '')
    ed.Command('._line', acge.Point3d(uniform(0, 100), uniform(0, 100), 0), acge.Point3d(uniform(0, 100), uniform(0, 100), 0), '')
    print '\nOK!'

@command()
def dbxtest():
    filename = 'd:\\test.dwg'
    with dbtrans(db = filename) as tr:
        btr = tr.openmodelspace()
        mat = acge.Matrix3d.Displacement(acge.Vector3d(10,0,0))
        for ent in (tr.getobject(eid) for eid in btr):
            with upopen(ent):
                if isinstance(ent, acdb.Line):
                    ent.LayerId = tr.LayerTable['1']
                elif isinstance(ent, acdb.DBText):
                    ent.TransformBy(mat)
        tr.Database.SaveAs(filename, acdb.DwgVersion.Current)

@command()
def cstest():
    with cs.opencprofile() as cpf:
        tpaths = cpf.Variables['TRUSTEDPATHS']
        s = r'c:\\nfox'
        patt = r'(^|;)%s(;|$)' % s
        import re
        m = re.search(patt, tpaths, re.IGNORECASE)
        if not m:
            print ';'.join((tpaths, s))


@command()
def mycir():
    with dbtrans() as tr:
        btr = tr.opencurrspace(acdb.OpenMode.ForWrite)
        res = ed.GetPoint('\n输入一个点')
        if res.Status == aced.PromptStatus.OK:
            cir = acdb.Circle(res.Value, acge.Vector3d.ZAxis, 2)
            tr.addentity(btr, cir)
            tr.setxrecord([[1000, 'abc']], cir, 'NFox.Cad')
            tr.setxrecord([[1000, 'abc']], 'NFox.Cad', 'test1')
            print '\n在对象字典中设置了值为%s的扩展数据' % tr.getxrecord(cir, 'NFox.Cad')
            print '\n在字典中设置了值为%s的扩展数据' % tr.getxrecord('NFox.Cad', 'test1')

@command()
@showtime
def mycir2():
    from random import uniform
    doc = acap.Application.DocumentManager.MdiActiveDocument
    db = doc.Database
    with db.TransactionManager.StartTransaction() as tr:
        btr = tr.GetObject(db.CurrentSpaceId, acdb.OpenMode.ForRead)
        btr.UpgradeOpen()
        cirs = (
            acdb.Circle(acge.Point3d(uniform(0, 10000), uniform(0, 10000), 0), acge.Vector3d.ZAxis, 2)
            for i in xrange(100000))
        for cir in cirs:
            btr.AppendEntity(cir)
            tr.AddNewlyCreatedDBObject(cir, True)
        btr.DowngradeOpen()
        tr.Commit()

@command()
@showtime
def mycir3():
    from random import uniform
    with dbtrans() as tr:
        btr = tr.opencurrspace()
        cirs = (
            acdb.Circle(acge.Point3d(uniform(0, 100), uniform(0, 100), 0), acge.Vector3d.ZAxis, 2)
            for i in xrange(100000))
        tr.addentity(btr, *cirs)

@command()
def myline():
    with dbtrans() as tr:
        #获取起点
        respt1 = ed.GetPoint('指定第一个点:')
        if respt1.Status != aced.PromptStatus.OK: return
        start = respt1.Value
        #获取第二点
        opts = aced.PromptPointOptions('\n指定下一点')
        opts.AllowNone = True
        opts.BasePoint = start
        opts.UseBasePoint = True
        opts.UseDashedLine = False
        respt2 = ed.GetPoint(opts)
        #循环生成直线并显示
        btr = tr.opencurrspace(acdb.OpenMode.ForWrite)
        while respt2.Status == aced.PromptStatus.OK:
            end = respt2.Value
            with acdb.Line(start, end) as line:
                tr.addentity(btr, line)
                tr.flush(line)
            #获取下一点
            opts.BasePoint = start = end
            respt2 = ed.GetPoint(opts)

@command()
def myline1():
    with dbtrans() as tr:
        btr = tr.opencurrspace()
        ents = []
        pts = []
        while True:
            n = len(pts)
            if n == 0:
                #获取起点
                respt1 = ed.GetPoint('指定第一个点:')
                if respt1.Status != aced.PromptStatus.OK: return
                pts.append(respt1.Value)
            else:
                if n > 2:
                    opts = aced.PromptPointOptions(
                        '\n 指定下一点或 [闭合(C)/放弃(U)]', 'C U')
                elif n > 0:
                    opts = aced.PromptPointOptions(
                        '\n 指定下一点或 [放弃(U)]', 'U')
                #获取下一点
                opts.AllowNone = True
                opts.BasePoint = pts[n - 1]
                opts.UseBasePoint = True
                opts.UseDashedLine = False
                respt2 = ed.GetPoint(opts)
                if respt2.Status == aced.PromptStatus.OK:
                    pt = respt2.Value
                    if n > 0:
                        line = acdb.Line(pts[n - 1], pt)
                        tr.addentity(btr, line)
                        tr.flush(line)
                        ents.append(line)
                        pts.append(pt)
                elif respt2.Status == aced.PromptStatus.Keyword:
                    if respt2.StringResult == 'C':
                        line = acdb.Line(pts[n - 1], pts[0])
                        tr.addentity(btr, line)
                        break
                    else:
                        del pts[n - 1]
                        if n > 1:
                            ents[n - 2].Erase()
                            tr.flush(line)
                            del ents[n - 2]
                else:
                    break

@command()
def mycir4():
    with dbtrans() as tr:
        btr = tr.opencurrspace(acdb.OpenMode.ForWrite)
        res = ed.GetEntity('\n选择一个圆')
        if res.Status == aced.PromptStatus.OK:
            cir = tr.getobject(res.ObjectId)
            tr.setxrecord([[1000, 'abc']], cir, 'NFox.Cad')
            print '\n在对象字典中设置了值为%s的扩展数据' % tr.getxrecord(cir, 'NFox.Cad')

@command()
def mypl3d():
    with dbtrans() as tr:
        btr = tr.opencurrspace(acdb.OpenMode.ForWrite)
        opts = aced.PromptEntityOptions('\n选择三维多段线')
        opts.SetRejectMessage('错误类型')
        opts.AddAllowedClass(acdb.Polyline3d, False)
        res = ed.GetEntity(opts)
        if res.Status == aced.PromptStatus.OK:
            pl = tr.getobject(res.ObjectId)
            for i in pl:
                print tr.getobject(i).Position

@command()
def myxdata():
    with dbtrans() as tr:
        ents = (acdb.Line(acge.Point3d(), acge.Point3d(10,10,0)))
        bdefid = tr.addblockdef('a', ents)
        btr = tr.opencurrspace()
        bref = acdb.BlockReference(acge.Point3d(), bdefid)
        tr.addentity(btr, bref)
        tr.addregapp('Do341')
        bref.XData = conv.ToBuffer(((1001, 'Do341'), (1000, 'test')))

@command()
def myxdata2():
    f = conv.ToFilter(((1001, 'De386'),))
    res = ed.SelectAll(f)
    print res.Status

def dimblktest():
    acap.Application.GetSystemVariable('dimblk')

@command()
def gecurvetest():
    res = ed.GetEntity('\n请选择多段线:')
    if res.Status == aced.PromptStatus.OK:
        with dbtrans() as tr:
            pl = tr.getobject(res.ObjectId)
            gepl = pl.GetGeCurve()
            cci = acge.CurveCurveIntersector3d(gepl, gepl, acge.Vector3d.ZAxis)
            for k in range(cci.NumberOfIntersectionPoints):
                pars = cci.GetIntersectionParameters(k)
                print gepl.EvaluatePoint(pars[0])
                
                
                